(function(){
    function SetUserLimit(){
        this.init(); 
    }

    SetUserLimit.prototype = {
        constructor:SetUserLimit,
        init:function(){
            this.nodata = '<tr><td  colspan="4">暂无信息</td></tr>';
            var _this = this;
        
                this.getUserList();
            
              

            
    

        },
        getUserList:function(){
            var _this = this;
            $.ajax({
                type:"get",
                url:"/admin/admin_users",
                dataType:"json",
                success:function(  info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code==1 )
                    {
                        var dataRow = info.data;
                        var strArr = [];
                        dataRow.forEach(function( item ){
                            var uname = item.username;
                            var optStr = '<option  value="'+uname+'">'+uname+'</option>';
                            strArr.push(optStr);
                        });
                        $("#userlist").html(strArr.join(""));
                        _this.getUserLimit();
                        $("#userlist").unbind().change(function(){
                            _this.getUserLimit();
                        });
                    }
                    else
                    {
                        $("#tipsContent").html(desc);
                        $("#loginoutModal").modal();
                    }

                }
            });
           
        },
         // 查询用户权限
     getUserLimit:function(){
        var _this = this;
        var userName = $("#userlist").val();
        $.ajax({
            type:"get",
            url:"/admin/query_auth?username="+userName,
            dataType:"json",
            success:function( info ){
                var code = info.code;
                var desc = info.desc;
                if( code==1 )
                {
                    var modules = info.modules;
                    var strArr = [];
                    modules.forEach(function(item){
                        var limitId = item.id;
                        var limitName = item.name;
                        var limitList = item.auths;
                        var html = ' <tr  class="limittitle"><td  colspan="4"><label  class="selectalllabel">全选 <input  type="checkbox" class="selectall"   value="'+limitId+'"></label>'+limitName+'<input  type="hidden"  class="parentauth"  value="'+limitId+'"></td></tr>';
                        var len = limitList.length;
                       //  数组长度 凑够 能够被4整除
                       var remain = len%4;
                       if( remain > 0 )
                       {
                          for( var i = 0 ;i < 4 -remain; i ++ )
                          {
                              limitList.push("");
                          }
                       }
                       limitList.forEach(function( value, key ){
                        var authId = value.id;
                        var authName = value.name;
                        var authCheck = value.check;
                        authCheck = authCheck?"checked" :"";
                            if( key%4 == 0 )
                            {
                                html += '<tr >';
                            }

                            if( value )
                            {
                                html += ' <td ><input type="checkbox"    value="'+authId+'"  '+authCheck+ ' class="limitshow  sellimit'+limitId+'"> '+authName+'</td>';
                            }
                            else
                            {
                                html += ' <td ></td>';
                            }
                            if( key%4 == 3 )
                            {
                                html += '</tr>';
                            }
                        });
                        strArr.push(html);
                    });
                    $("#unticketList").html( strArr.join("") );
                     // 全选 全取消
                     $(".selectall").click(function(){
                        var selValue = $(this).val();
                        var isProp = $(this).prop("checked");
                        if( isProp )
                        {
                            $(".sellimit"+selValue).prop("checked", true);
                        }
                        else
                        {
                            $(".sellimit"+selValue).prop("checked", false);
                        }
                    });
                    //  设置权限
                    $(".setlimitbox  button").unbind("click").click(function(){
                        _this.setUserLimit();
                    });
                     

                }
                else
                {
                    $("#tipsContent").html(desc);
                    $("#loginoutModal").modal();
                }

            }
        });

     },
     setUserLimit:function(){
         var setlimitArr = [];
         var userName = $("#userlist").val();
         $(".parentauth").each(function(){
            var parentId = $(this).val();//模块的id
            var authIdArr = [];//存储每个模块下面选择设置的 权限id
            $(".sellimit"+parentId + ":checked").each(function(){
              authIdArr.push( $(this).val() );
            });
            if( authIdArr.length )
            {
              var selStr = parentId+":"+authIdArr.join(",");
              setlimitArr.push(selStr);
            }
           
        });
        var moduleId = setlimitArr.join(";");
        var pData = {
            username:userName,
            auth:moduleId
        };
        $.ajax({
            type:"post",
            url:"/admin/set_auth",
            data:pData,
            success:function( info ){
                var code = info.code;
                var desc = info.desc;
                if( code == 1 )
                {
                    $("#tipsContent").html("设置成功！");
                    $("#loginoutModal").modal();

                }
                else
                {
                    $("#tipsContent").html(desc);
                    $("#loginoutModal").modal();
                }


            }
        });

     }




   
  
  
   
      
      
    };





  new SetUserLimit();

}());